package dwl.graduation.design.controller;

import dwl.graduation.design.domain.Permission;
import dwl.graduation.design.domain.PermissionInsertRequest;
import dwl.graduation.design.domain.PermissionUpdateRequest;
import dwl.graduation.design.domain.Role;
import dwl.graduation.design.service.PermissionService;
import dwl.graduation.design.utils.MessageConstant;
import dwl.graduation.design.utils.PageResult;
import dwl.graduation.design.utils.QueryPageBean;
import dwl.graduation.design.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/permission")
public class PermissionController {
    private final PermissionService permissionService;

    @Autowired
    public PermissionController(PermissionService permissionService){
        this.permissionService = permissionService;
    }

    @RequestMapping(value = "/findPermissionList", method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    public PageResult findPermissionList(@RequestBody QueryPageBean queryPageBean) {
        return permissionService.findPermissionList(queryPageBean);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    public Result insertPermission(@RequestBody PermissionInsertRequest request) {
        try {
            boolean flag = permissionService.insertPermission(request);
            if (flag) {
                return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasRole('ADMIN')")
    public Result deletePermission(@RequestParam("id") Integer id) {
        try {
            permissionService.deletePermission(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    public Result updatePermission(@RequestBody PermissionUpdateRequest roleUpdateRequest) {
        try {
            boolean flag = permissionService.updatePermission(roleUpdateRequest);
            if(flag){
                return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
            }else{
                return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    @RequestMapping(value = "/findPermissionById",method = RequestMethod.GET)
    public Result getUserInfoByUserId(Integer id){
        try{
            Permission permission = permissionService.findPermissionById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,permission);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
}
